package com.yuanfeng.base.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.NavSortLogoVO;
import com.yuanfeng.base.dto.SystemConfigHallDTO;
import com.yuanfeng.base.dto.WebConfigDTO;
import com.yuanfeng.base.entity.BaseSysConfigsEntity;
import com.yuanfeng.base.entity.BaseSystemWebConfigEntity;
import com.yuanfeng.base.entity.BaseWebConfigEntity;
import com.yuanfeng.base.feign.OrderClient;
import com.yuanfeng.base.feign.UsermsClient;
import com.yuanfeng.base.mapper.BaseSysConfigsMapper;
import com.yuanfeng.base.mapper.BaseSystemWebConfigMapper;
import com.yuanfeng.base.mapper.BaseWebConfigMapper;
import com.yuanfeng.base.service.BaseSysConfigsService;
import com.yuanfeng.base.vo.SystemConfigHallSlideVO;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.dto.goods.NavSortLogoDTO;
import com.yuanfeng.commoms.dto.userms.UserTokenBeanDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.Json.JsonUtils;
import com.yuanfeng.commoms.util.*;
import com.yuanfeng.commoms.util.secverifyapiutil.Base64Utils;
import com.yuanfeng.commoms.vo.base.PlatformoperationVO;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName BaseSysConfigsServiceImpl
 * @Description
 * @Author cyp
 * @Date 2023-06-13 09:05:20
 * @Version 1.0
 **/
@Service
public class BaseSysConfigsServiceImpl extends ServiceImpl<BaseSysConfigsMapper, BaseSysConfigsEntity>
        implements BaseSysConfigsService {

    @Autowired
    private BaseSysConfigsMapper baseSysConfigsMapper;
    @Autowired
    private BaseWebConfigMapper baseWebConfigMapper;
    @Autowired
    private BaseSystemWebConfigMapper baseSystemWebConfigMapper;
    @Autowired
    private UsermsClient usermsClient;
    @Value("${domainName}")
    private String domain;
    @Autowired
    private OrderClient orderClient;


    @Override
    public String selectConfigValueFeign(String configKey) {
        return baseSysConfigsMapper.selectConfigValue(configKey);
    }

    @Override
    public ResponseResult quertNavLogo(NavSortLogoDTO gdbNavSortLogoDTO) {
        List<BaseSysConfigsEntity> webNavLogoConbfigDTOList = new ArrayList<>();
        //如果ConfigKey 不是null那证明是单查 从redis获取redis中不存在查询数据库
        if (null != gdbNavSortLogoDTO && StrUtil.isNotBlank(gdbNavSortLogoDTO.getConfigKey())) {
            String s = RedisUtil.get("SYSTEM_" + gdbNavSortLogoDTO.getConfigKey());
            if (null != s) {
                BaseSysConfigsEntity webNavLogoConbfigDTO = new BaseSysConfigsEntity();
                webNavLogoConbfigDTO.setConfigKey(gdbNavSortLogoDTO.getConfigKey());
                webNavLogoConbfigDTO.setConfigValue(s);
                webNavLogoConbfigDTOList.add(webNavLogoConbfigDTO);
                return ResponseResult.success(webNavLogoConbfigDTOList);
            }
        }
        if (null != gdbNavSortLogoDTO && StrUtil.isNotBlank(gdbNavSortLogoDTO.getConfigType())) {
            String s = RedisUtil.get("SYSTEM_" + gdbNavSortLogoDTO.getConfigType());
            if (StrUtil.isNotBlank(s)) {
                webNavLogoConbfigDTOList = JSON.parseArray(s,BaseSysConfigsEntity.class);
                return ResponseResult.success(webNavLogoConbfigDTOList);
            }
        }
        webNavLogoConbfigDTOList = this.baseMapper.selectList(new LambdaQueryWrapper<BaseSysConfigsEntity>()
                .eq(!StringUtils.isEmpty(gdbNavSortLogoDTO.getConfigType()), BaseSysConfigsEntity::getConfigType, gdbNavSortLogoDTO.getConfigType())
                .eq(!StringUtils.isEmpty(gdbNavSortLogoDTO.getConfigKey()), BaseSysConfigsEntity::getConfigKey, gdbNavSortLogoDTO.getConfigKey())
                .orderByAsc(BaseSysConfigsEntity::getConfigSort));
        RedisUtil.set("SYSTEM_" + gdbNavSortLogoDTO.getConfigType(), JSON.toJSONString(webNavLogoConbfigDTOList));
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        List<BaseSysConfigsEntity> finalWebNavLogoConbfigDTOList = webNavLogoConbfigDTOList;
        executorService.submit(() -> {
            if (null != finalWebNavLogoConbfigDTOList) {
                for (BaseSysConfigsEntity webNavLogoConbfigDTO : finalWebNavLogoConbfigDTOList) {
                    RedisUtil.set("SYSTEM_" + webNavLogoConbfigDTO.getConfigKey(), webNavLogoConbfigDTO.getConfigValue());
                }
            }
        });
        // 关闭线程池
        executorService.shutdown();
        return ResponseResult.success(webNavLogoConbfigDTOList);
    }

    @Override
    public ResponseResult getImageConfigUrl(String configKey) {

        if (configKey == null || "".equals(configKey)) {
            return ResponseResult.fail(BizCodeEnume.KEY_VALUE_CANNOT_BE_EMPTY.getMsg());
        }
        BaseSysConfigsEntity merchantSysConfigsEntity = new BaseSysConfigsEntity();

        merchantSysConfigsEntity = this.baseMapper.selectOne(new QueryWrapper<BaseSysConfigsEntity>().eq("config_key", configKey));


        String imageValue = merchantSysConfigsEntity.getConfigValue();

        return ResponseResult.success(imageValue);

    }

    @Override
    public ResponseResult selectByConfigKey(String configKey) {
        if (configKey == null || "".equals(configKey)) {
            return ResponseResult.fail(BizCodeEnume.KEY_VALUE_CANNOT_BE_EMPTY.getMsg());
        }
        BaseSysConfigsEntity entity = this.baseMapper.selectOne(new QueryWrapper<BaseSysConfigsEntity>()
                .eq("config_key", configKey));
        if (CommonType.configKey.THEME_WEB_COLOR.getDesc().equals(configKey)) {
            entity.setConfigComment(domain + "/pc/");
        }

        return ResponseResult.success(entity);
    }

    @Override
    public ResponseResult queryIntegralIndex(Map<String, String> param) {
        Map<String, Object> resMap = new HashMap<>();
        Map<String, Object> resultmap = new HashMap<>();
        try {
            List<BaseSysConfigsEntity> list = this.baseMapper.queryIntegralIndex("integral_image");
            if (null != param && null != param.get("token")) {
                UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.get("token"));
                ResponseResult userMemberInformation = usermsClient.getUserMemberInformation(param);
                if (null != userMemberInformation) {
                    resultmap = (Map<String, Object>) userMemberInformation.getData();
                    resultmap.put("distanceIntegral", resultmap.get("nextGradeRule"));
                    if (null != userInfo.getUserNickname() && !"".equals(userInfo.getUserNickname())) {
                        resultmap.put("userName", userInfo.getUserNickname());
                    }
                }
                if (null != userInfo) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("userId", userInfo.getUserId());
                    ResponseResult resp1 = orderClient.queryIntegraOrdersCount(map);
                    if (null != resp1.getData()) {
                        Integer remap = (Integer) resp1.getData();
                        resultmap.put("orderCount", remap);
                    }
                }
            }
            resMap.put("resultmap", resultmap);
            resMap.put("list", list);
            return ResponseResult.success(resMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.fail(BizCodeEnume.SYSTEM_ERROR.getCode(), BizCodeEnume.SYSTEM_ERROR.getMsg());
    }

    @Override
    public ResponseResult queryPhoneAndMail(UserTokenBeanDTO param) {
        List<SystemConfigHallSlideVO> slides = null;
        slides = baseSysConfigsMapper.selectPhoneAndMail();
        return ResponseResult.success(slides);

    }

    @Override
    public ResponseResult updateConfigEnableByConfigKey(String configKey, String configEnable) {
        this.baseMapper.update(null, new LambdaUpdateWrapper<BaseSysConfigsEntity>()
                .set(BaseSysConfigsEntity::getConfigEnable, configEnable)
                .eq(BaseSysConfigsEntity::getConfigKey, configKey));
        return ResponseResult.success();
    }

    @Override
    public ResponseResult querySupplyIndexSlide(Map<String, Object> param) {
        List<BaseSysConfigsEntity> slides = this.baseMapper.selectList(new LambdaQueryWrapper<BaseSysConfigsEntity>()
                .eq(BaseSysConfigsEntity::getConfigType, param.get("configType").toString())
                .orderByAsc(BaseSysConfigsEntity::getConfigSort));
        for (BaseSysConfigsEntity slide : slides) {
            if (slide.getOpType().equals("2")) {
                //Integer goodsId = JsonUtils.string2Obj(slide.getOpValue(), GetCommonIdByGoodsIdVO.class).getGoodsId();
                String goodsId = String.valueOf(slide.getOpValue());
                slide.setOpValue(goodsId);
            }

            if (slide.getOpType().equals("3")) {
                String url = slide.getOpValue();
                slide.setOpValue(url);
            }

            String opValue = slide.getOpValue();
            Map map;
            if ((slide.getConfigType().equals(CommonType.configType.SUPPLY_PC_INDEX_AD.getDesc())
                    || slide.getConfigType().equals(CommonType.configType.PC_INDEX_AD.getDesc())
            )
                    && slide.getOpType().equals("2")) {
                map = JSON.parseObject(opValue, Map.class);
                String commonName = this.baseMapper.selectCommonName(Integer.valueOf(String.valueOf(map.get("commonId"))));
                slide.setCommonName(commonName);
            }
            if (slide.getConfigType().equals(CommonType.configType.INDEX_SLIDER.getDesc())
                    && slide.getOpType().equals("2")) {
                String commonName = this.baseMapper.selectCommonName(Integer.valueOf(slide.getOpValue()));
                slide.setCommonName(commonName);
            } else if ((slide.getConfigType().equals(CommonType.configType.SUPPLY_PC_INDEX_AD.getDesc()) || slide.getConfigType().equals(CommonType.configType.PC_INDEX_AD.getDesc())) && slide.getOpType().equals("4")) {
                map = JSON.parseObject(opValue, Map.class);
                String shopName = this.baseMapper.selectByShopName(Integer.valueOf(String.valueOf(map.get("shopId"))));
                slide.setShopName(shopName);
            } else if ((slide.getConfigType().equals(CommonType.configType.SUPPLY_PC_INDEX_AD.getDesc()) || slide.getConfigType().equals(CommonType.configType.PC_INDEX_AD.getDesc())) && slide.getOpType().equals("6")) {
                map = JSON.parseObject(opValue, Map.class);
                String catName = this.baseMapper.selectByCatNameString(Integer.valueOf(String.valueOf(map.get("catId"))));
                slide.setCatName(catName);
            }


        }

        return ResponseResult.success(slides);
    }

    @Override
    public ResponseResult updateSysConfigByKey(SystemConfigHallDTO dto) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(dto.getToken());
        List<BaseSysConfigsEntity> configs = dto.getSystemConfigAddParam();
        if (null != userInfo) {
            for (BaseSysConfigsEntity config : configs) {
                if ("2".equals(config.getOpType()) && config.getOpValue().contains(",")) {
                    return ResponseResult.fail(BizCodeEnume.VALID_EXCEPTION);
                }
                config.setCreateDate(new Date());
                config.setUpdateDate(new Date());
                config.setCreateBy(userInfo.getUserId());
                config.setUpdateBy(userInfo.getUserId());
            }
            this.baseMapper.updateSetting(configs);
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult querySysConfigByKey(Map<String, Object> map) {
        List<BaseSysConfigsEntity> slides = this.baseMapper.selectList(new LambdaQueryWrapper<BaseSysConfigsEntity>()
                .eq(BaseSysConfigsEntity::getConfigType, map.get("key").toString())
                .orderByAsc(BaseSysConfigsEntity::getConfigSort));
        if (CommonType.configKey.THEME_UNI_COLOR.getDesc().equals(map.get("key").toString())) {
            WebConfigDTO webConfig = new WebConfigDTO();
            BeanUtils.copyProperties(slides.get(0), webConfig);
            List<String> wapColorList = ObjectChangeList.castObjToList(JSON.parse(slides.get(0).getConfigValue()), String.class);
            List<Integer> setList = ObjectChangeList.castObjToList(JSON.parse(slides.get(0).getOpValue()), Integer.class);
            webConfig.setColorList(wapColorList);
            webConfig.setSetList(setList);
            webConfig.setConfigComment(domain + "/h5/");
            return ResponseResult.success(webConfig);
        } else {
            return ResponseResult.success(slides);
        }
    }

    @Override
    public ResponseResult getConfigValue(String configKey, String configType) {
        if (configKey == null || "".equals(configType)) {
            return ResponseResult.fail(BizCodeEnume.KEY_VALUE_CANNOT_BE_EMPTY.getMsg());
        }


        BaseSysConfigsEntity merchantSysConfigsEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<BaseSysConfigsEntity>()
                .eq(BaseSysConfigsEntity::getConfigKey, configKey).eq(BaseSysConfigsEntity::getConfigType, configType));


        String configValue = merchantSysConfigsEntity.getConfigValue();

        return ResponseResult.success(configValue);
    }

    @Override
    public ResponseResult queryThemeH5ColorSetting(Map<String, Object> param) {
        List<BaseSysConfigsEntity> slides = this.baseMapper.selectList(new LambdaQueryWrapper<BaseSysConfigsEntity>()
                .eq(BaseSysConfigsEntity::getConfigType, param.get("key").toString())
                .orderByAsc(BaseSysConfigsEntity::getConfigSort));
        if (CommonType.configKey.THEME_UNI_COLOR.getDesc().equals(param.get("key").toString())) {
            WebConfigDTO webConfig = new WebConfigDTO();
            BeanUtils.copyProperties(slides.get(0), webConfig);
            List<String> wapColorList = ObjectChangeList.castObjToList(JSON.parse(slides.get(0).getConfigValue()), String.class);
            List<Integer> setList = ObjectChangeList.castObjToList(JSON.parse(slides.get(0).getOpValue()), Integer.class);
            webConfig.setColorList(wapColorList);
            webConfig.setSetList(setList);
            webConfig.setConfigComment(domain + "/h5/");
            return ResponseResult.success(webConfig);
        } else {
            return ResponseResult.success(slides);
        }
    }

    @Override
    public ResponseResult updateThemeColorPageSetting(WebConfigDTO webConfig) {
        //设置主题色的key和值
        LambdaUpdateWrapper<BaseSysConfigsEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(BaseSysConfigsEntity::getConfigValue, webConfig.getConfigValue());
        if (CommonType.configKey.THEME_UNI_COLOR.getDesc().equals(webConfig.getConfigKey())) {
            updateWrapper.set(BaseSysConfigsEntity::getOpValue, webConfig.getConfigRemarks());
        }
        updateWrapper.eq(BaseSysConfigsEntity::getConfigKey, webConfig.getConfigKey());
        this.baseMapper.update(null, updateWrapper);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult updateNavCommonSortLogo(NavSortLogoVO gdbNavSortLogoDTOs) {
        List<NavSortLogoDTO> logos = gdbNavSortLogoDTOs.getLogos();
        if (logos != null) {
            for (NavSortLogoDTO logo : logos) {
                //update
                Integer updateNav = this.baseMapper.update(null, new LambdaUpdateWrapper<BaseSysConfigsEntity>()
                        .set(BaseSysConfigsEntity::getConfigValue, logo.getConfigValue())
                        .eq(BaseSysConfigsEntity::getConfigKey, logo.getConfigKey()));
                if (updateNav == 1) {
                    RedisUtil.set("SYSTEM_" + logo.getConfigKey(), logo.getConfigValue());
                    RedisUtil.remove("SYSTEM_logo_copywriting");
                }
            }
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult updateNavCommonSortLogo2(Map<String, String> map) {
        if (null != map && map.get("logos") != null) {
            String fromBase64 = null;
            try {
                String data = map.get("logos");
                fromBase64 = Base64Utils.getFromBase64(data);

            } catch (Exception e) {
                e.printStackTrace();
                return ResponseResult.fail(BizCodeEnume.DECODING_FAILED.getCode(), BizCodeEnume.DECODING_FAILED.getMsg());
            }
            List<NavSortLogoDTO> logos = JsonUtils.string2Obj(fromBase64, List.class, NavSortLogoDTO.class);
            if (null != logos && logos.size() > 0) {
                for (NavSortLogoDTO logo : logos) {
                    int update = this.baseMapper.update(null, new LambdaUpdateWrapper<BaseSysConfigsEntity>()
                            .set(BaseSysConfigsEntity::getConfigValue, logo.getConfigValue())
                            .eq(BaseSysConfigsEntity::getConfigKey, logo.getConfigKey()));
                    if (update == 1) {
                        RedisUtil.set("SYSTEM_" + logo.getConfigKey(), logo.getConfigValue());
                        RedisUtil.remove("SYSTEM_logo_copywriting");
                    }
                }
            }
            return ResponseResult.success();
        } else {
            return ResponseResult.fail(BizCodeEnume.MISSING_PARAMETER.getCode(), BizCodeEnume.MISSING_PARAMETER.getMsg());
        }
    }

    @Override
    public ResponseResult queryadvertising(Map<String, Object> param) {
        String token = (String) param.get("token");
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(null, token);
        Integer integer = null;
        Integer shopId = null;
        if (userPermissionsResult.getFlag()) {
            // 根据code存储用户信息
            UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
            if (StringUtils.isNotEmpty(userInfo.getShopId())) {
                shopId = Integer.valueOf(userInfo.getShopId());
            }

        }
        String configKey = CommonType.configKey.WEB_ME_PAGE_ADVERTISING.getDesc() + "_" + shopId;
        BaseSysConfigsEntity systemConfigHallSlideResults = this.baseMapper.selectOne(new LambdaQueryWrapper<BaseSysConfigsEntity>()
                .eq(BaseSysConfigsEntity::getConfigKey, configKey));

        if (systemConfigHallSlideResults != null) {
            return ResponseResult.success(systemConfigHallSlideResults);
        }
        return ResponseResult.success(2);
    }

    @Override
    public ResponseResult updateAdvertising(Map<String, Object> param) {
        String token = (String) param.get("token");
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(null, token);
        Integer shopId = null;
        if (userPermissionsResult.getFlag()) {
            // 根据code存储用户信息
            UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
            if (StringUtils.isNotEmpty(userInfo.getShopId())) {
                shopId = Integer.valueOf(userInfo.getShopId());
            }

        }
        String configKey = CommonType.configKey.WEB_ME_PAGE_ADVERTISING.getDesc() + "_" + shopId;

        BaseSysConfigsEntity systemConfigHallSlideResults = this.baseMapper.selectOne(new LambdaQueryWrapper<BaseSysConfigsEntity>()
                .eq(BaseSysConfigsEntity::getConfigKey, configKey));
        BaseSysConfigsEntity configs = ParseMapUtils.parseMap2Object(param, BaseSysConfigsEntity.class);
        configs.setConfigKey(configKey);
        configs.setConfigType(configKey);
        if (systemConfigHallSlideResults != null) {
            this.baseMapper.update(configs, new LambdaUpdateWrapper<BaseSysConfigsEntity>()
                    .eq(BaseSysConfigsEntity::getConfigKey, configKey));
            return ResponseResult.success();
        } else {
            this.baseMapper.insert(configs);
            return ResponseResult.success();
        }
    }

    @Override
    public ResponseResult updatehotword(Map<String, Object> param) {
        String token = (String) param.get("token");
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(null, token);
        Integer integer = null;
        Integer shopId = null;
        if (userPermissionsResult.getFlag()) {
            // 根据code存储用户信息
            UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
            if (StringUtils.isNotEmpty(userInfo.getShopId())) {
                shopId = Integer.valueOf(userInfo.getShopId());
            }
            String configKey = "brandshop_hot_word_" + shopId;

            BaseSysConfigsEntity entity = this.baseMapper.selectOne(new LambdaQueryWrapper<BaseSysConfigsEntity>()
                    .eq(BaseSysConfigsEntity::getConfigKey, configKey));

            String configValue = String.valueOf(param.get("configValue"));
            List<String> result1 = Arrays.asList(StringUtils.split(configValue, ","));
            if (result1.size() > 9) {
                return ResponseResult.fail(BizCodeEnume.PLATFROM_SEARCH_UPDATE_ERROP);
            }

            if (entity == null) {
                BaseSysConfigsEntity shophot1 = new BaseSysConfigsEntity();
                shophot1.setConfigValue(configValue);
                shophot1.setConfigKey(configKey);
                shophot1.setConfigComment("品牌店铺热搜词");
                shophot1.setOpType("5");
                shophot1.setOpValue("");
                shophot1.setConfigSort(1);
                shophot1.setConfigType("search");
                shophot1.setConfigEnable(1);
                shophot1.setTerminalType(2);
                shophot1.setCreateBy(userInfo.getUserName());
                shophot1.setUpdateBy(userInfo.getUserName());
                integer = this.baseMapper.insert(shophot1);
            } else {
                //更新数据
                entity.setConfigValue(configValue);
                integer = this.baseMapper.updateById(entity);
            }

        }
        if (integer > 0) {
            return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
        } else {
            return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED);
        }
    }


    @Override
    public ResponseResult updateAdPage(SystemConfigHallDTO param) {
        List<BaseSysConfigsEntity> configs = param.getSystemConfigAddParam();
        for (BaseSysConfigsEntity config : configs) {
            config.setCreateDate(new Date());
            config.setUpdateDate(new Date());
            config.setCreateBy("0");
            config.setUpdateBy("0");
            config.setConfigEnable(1);
            this.baseMapper.update(config, new LambdaUpdateWrapper<BaseSysConfigsEntity>()
                    .eq(BaseSysConfigsEntity::getConfigKey, config.getConfigKey()));
        }
        return ResponseResult.success(1);
    }

    @Override
    public ResponseResult queryMePage() {
        List<BaseSysConfigsEntity> slides = this.baseMapper.selectList(new LambdaQueryWrapper<BaseSysConfigsEntity>()
                .eq(BaseSysConfigsEntity::getConfigType, CommonType.configKey.WEB_ME_PAGE_ADVERTISING.getDesc())
                .orderByAsc(BaseSysConfigsEntity::getConfigSort));
        if (null != slides) {
            return ResponseResult.success(slides.get(0));
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult queryWapColorForUniApp() {
        BaseWebConfigEntity webConfig = baseWebConfigMapper.selectById("uni_color");
        List<String> wapColorList = ObjectChangeList.castObjToList(JSON.parse(webConfig.getConfigValue()), String.class);
        List<Integer> setList = ObjectChangeList.castObjToList(JSON.parse(webConfig.getConfigRemarks()), Integer.class);
        webConfig.setColorList(wapColorList);
        webConfig.setSetList(setList);
        webConfig.setConfigComment(domain + "/h5/");
        return ResponseResult.success(webConfig);
    }

    @Override
    public ResponseResult queryplatformoperation() {
        List<BaseSysConfigsEntity> pos = this.baseMapper.selectList(new LambdaQueryWrapper<BaseSysConfigsEntity>()
                .eq(BaseSysConfigsEntity::getConfigType, CommonType.configType.PLATFORM_OPERATION_SPECIFICATIONS.getDesc()));
        PlatformoperationVO platformoperationEntity = new PlatformoperationVO();
        if (pos != null && pos.size() > 0) {
            String configValue = pos.get(0).getConfigValue();
            platformoperationEntity = JSONObject.parseObject(configValue, PlatformoperationVO.class);
        }
        platformoperationEntity.setConfigKey(CommonType.configType.PLATFORM_OPERATION_SPECIFICATIONS.getDesc());
        return ResponseResult.success(platformoperationEntity);

    }

    @Override
    public ResponseResult updateoperation(Map<String, Object> map) {

        boolean isExistToken = RedisUtil.exists(YFConfig.YF_PLATFORM_TOKEN_USER + String.valueOf(map.get("token")));

        if (!isExistToken) {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
        String fromBase64 = null;
        if (map.get("explanation") != null && map.get("explanation") != "") {
            fromBase64 = Base64Utils.getFromBase64(String.valueOf(map.get("explanation")));
        }
        PlatformoperationVO platformoperationEntity =
                JSONObject.parseObject(JSONObject.toJSONString(map.get("operation")), PlatformoperationVO.class);
        platformoperationEntity.setExplanation(fromBase64);

        String operation = JSONObject.toJSONString(platformoperationEntity);
        BaseSysConfigsEntity sys = new BaseSysConfigsEntity();
        List<BaseSysConfigsEntity> configs = new ArrayList<>();
        sys.setConfigKey(String.valueOf(map.get("configKey")));
        sys.setConfigValue(operation);
        sys.setConfigComment("平台管理规范");
        sys.setOpValue("");
        sys.setUpdateDate(new Date());
        configs.add(sys);
        sys = this.baseMapper.selectById(String.valueOf(map.get("configKey")));
        if (sys != null) {
            this.updateBatchById(configs);
        } else {
            this.saveBatch(configs);
        }
        return ResponseResult.success(BizCodeEnume.UPDATE_SUCCEEDED);
    }

    @Override
    public ResponseResult updatecatmoney(Map<String, Object> map) {
        boolean isExistToken = RedisUtil.exists(YFConfig.YF_PLATFORM_TOKEN_USER + String.valueOf(map.get("token")));

        if (!isExistToken) {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
        BaseSysConfigsEntity sys = new BaseSysConfigsEntity();
        sys.setConfigKey(CommonType.configKey.RETENTION_MONEY_DEFAULT_LIMIT.getDesc());
        sys.setConfigValue(String.valueOf(map.get("amount")));
        this.baseMapper.updateById(sys);

        return ResponseResult.success(BizCodeEnume.UPDATE_SUCCEEDED);
    }

    @Override
    public ResponseResult querycatmoney() {
        List<BaseSysConfigsEntity> pos =
                this.baseMapper.selectList(new LambdaQueryWrapper<BaseSysConfigsEntity>()
                        .eq(BaseSysConfigsEntity::getConfigType, CommonType.configKey.RETENTION_MONEY_DEFAULT_LIMIT.getDesc()));
        String amount = null;
        if (pos != null && pos.size() > 0) {
            amount = pos.get(0).getConfigValue();
        }
        return ResponseResult.success("质保金默认额度", amount);
    }


    @Override
    public ResponseResult selecthotword(Map<String, Object> param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        Integer shopId = (Integer) param.get("shopId");
        if (StringUtils.isNotEmpty(userInfo.getShopId())) {
            shopId = Integer.valueOf(userInfo.getShopId());
        }
        String config_key = "brandshop_hot_word_" + shopId;
        new BaseSysConfigsEntity();
        BaseSysConfigsEntity merchantSysConfigsEntity = this.baseMapper.selectOne(new QueryWrapper<BaseSysConfigsEntity>().eq("config_key", config_key));

        if (merchantSysConfigsEntity != null) {
            return ResponseResult.success(merchantSysConfigsEntity.getConfigValue());
        }
        return ResponseResult.fail(0, "店铺没有设置");
    }

    @Override
    public ResponseResult businessPasswordConfig() {
        Map<String, String> returnMap = new HashMap<>();
        BaseSystemWebConfigEntity merchantSysConfigsEntity;
        merchantSysConfigsEntity = baseSystemWebConfigMapper.selectOne(new QueryWrapper<BaseSystemWebConfigEntity>().eq("config_key", "password_length"));
        String passwordLength = merchantSysConfigsEntity.getConfigValue();
        merchantSysConfigsEntity = baseSystemWebConfigMapper.selectOne(new QueryWrapper<BaseSystemWebConfigEntity>().eq("config_key", "password_format"));
        String passwordFormat = merchantSysConfigsEntity.getConfigValue();
        String desc = "密码只能为" + passwordLength + "位";
        if ("1".equals(passwordFormat)) {
            desc = desc + "纯数字！";
        } else if ("2".equals(passwordFormat)) {
            desc = desc + "纯英文！";
        } else if ("3".equals(passwordFormat)) {
            desc = desc + "数字和英文的组合！";
        } else if ("4".equals(passwordFormat)) {
            desc = desc + "数字、英文和符号的组合！";
        }
        returnMap.put("passwordFormat", passwordFormat);
        returnMap.put("passwordLength", passwordLength);
        returnMap.put("desc", desc);
        return ResponseResult.success(returnMap);
    }

    @Override
    public ResponseResult queryBusinessConfig() {
        BaseSystemWebConfigEntity merchantSysConfigsEntity = baseSystemWebConfigMapper.selectOne(
                new QueryWrapper<BaseSystemWebConfigEntity>().eq("config_key", "business_switch"));
        return ResponseResult.success(merchantSysConfigsEntity.getConfigValue());
    }
}
